#coding:utf-8
# -*- coding: utf-8 -*-
#@Time : 2020/12/22 10:56
#@Author: 王鹏-wangpeng99
#@File : uploadutils.py

#coding:utf-8
# -*- coding: utf-8 -*-
#@Time : 2020/12/4 21:03
#@Author: 王鹏-wangpeng99
#@File : Test.py

from config import config
from datetime import datetime
import requests
from faker import Faker
import json
import traceback
from utils import dtutils,logutils

logger = logutils.getlogger(__file__)
zhfaker = Faker(locale='zh-CN')

voltCurrentTemlplate="""{
  "output": "output1.children",
  "outputs": {
    "id": "output1",
    "property": "children"
  },
  "inputs": [
    {
      "id": "voltUploadBtn",
      "property": "n_clicks",
      "value": 2
    }
  ],
  "changedPropIds": [
    "voltUploadBtn.n_clicks"
  ],
  "state": [
    {
      "id": "volt-ts-input",
      "property": "value",
      "value": "2020-12-21 17:23:45"
    },
    {
      "id": "volt-input",
      "property": "value",
      "value": "0"
    },
    {
      "id": "current-input",
      "property": "value",
      "value": "0"
    }
  ]
}"""

wifiTemplate="""{
  "output": "output2.children",
  "outputs": {
    "id": "output2",
    "property": "children"
  },
  "inputs": [
    {
      "id": "wifiUploadBtn",
      "property": "n_clicks",
      "value": 1
    }
  ],
  "changedPropIds": [
    "wifiUploadBtn.n_clicks"
  ],
  "state": [
    {
      "id": "wifi-ts-input",
      "property": "value",
      "value": "2020-12-21 17:23:45"
    },
    {
      "id": "link-input",
      "property": "value",
      "value": "3"
    },
    {
      "id": "level-input",
      "property": "value",
      "value": "-2"
    },
    {
      "id": "noise-input",
      "property": "value",
      "value": "-5"
    }
  ]
}"""

def uploadVoltCurrent(dtstr=dtutils.currentTimeStampStr(),volt=0.0,current=0):
    jsondata = json.loads(voltCurrentTemlplate)
    for item in jsondata['state']:
        if item['id']=='volt-ts-input':
            item['value'] = dtstr
        if item['id'] == 'volt-input':
            item['value'] = str(volt)
        if item['id'] == 'current-input':
            item['value'] = str(current)
    url="http://{}:8050/_dash-update-component".format(config.UploadConfig.host)
    try:
        res = requests.post(url=url, json=jsondata)
        logger.info("res={}".format(res.text()))
    except Exception:
        traceback.print_exc()
        logger.error(traceback.format_exc())

def uploadVoltCurrent(dtstr=dtutils.currentTimeStampStr(),link=0,level=0,noise=0):
    jsondata = json.loads(wifiTemplate)
    for item in jsondata['state']:
        if item['id'] == 'wifi-ts-input':
            item['value'] = dtstr
        if item['id'] == 'link-input':
            item['value'] = str(link)
        if item['id'] == 'level-input':
            item['value'] = str(level)
        if item['id'] == 'noise-input':
            item['value'] = str(noise)
    url="http://{}:8050/_dash-update-component".format(config.UploadConfig.host)
    try:
        res = requests.post(url=url, json=jsondata)
        logger.info("res={}".format(res.text()))
    except Exception:
        traceback.print_exc()
        logger.error(traceback.format_exc())


